package com.guo.pet_store.controller;

import com.guo.pet_store.admin.pojo.Order;
import com.guo.pet_store.admin.pojo.Pet;
import com.guo.pet_store.admin.pojo.User;
import com.guo.pet_store.admin.service.OrderService;
import com.guo.pet_store.admin.service.PetService;
import com.guo.pet_store.admin.service.ShopService;
import com.guo.pet_store.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/car")
public class CarController {
    //根据商品的id和session中的用户id添加到购物车中
    @Autowired
    PetService petService;
    @Autowired
    ShopService shopService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;


    @ResponseBody
    @GetMapping("/setCount")
    public Map setCount(HttpSession session, int pet_id, int count){
        System.out.println("pet_id"+pet_id+"count"+count);
        User user =(User) session.getAttribute("user");
        System.out.println("pet_id "+pet_id+" carid "+user.getShopCar().getId()+" count "+count);
        Long userId = user.getId();
        petService.setCount(pet_id,user.getShopCar().getId(),count);
        HashMap<String, String> map = new HashMap<>();
        map.put("message","成功");
        return map;
    }

    @RequestMapping
    public String getCar(Model model,HttpSession httpSession,RedirectAttributes redirectAttributes){
        User user = (User) httpSession.getAttribute("user");
        if (user ==null){
            redirectAttributes.addFlashAttribute("message","登陆后才能查看自己的购物车哦");
            return "login";
        }else {
            Double totalPrice  =0.00;
            List<Pet> allItems = shopService.findAllItems(user.getShopCar().getId());
            for (Pet allItem : allItems) {
                totalPrice += allItem.getPrice();
            }
            List<Integer> numbers = shopService.findPetitems(user.getShopCar().getId());
            model.addAttribute("user",httpSession.getAttribute("user"));
            model.addAttribute("items",allItems);
            model.addAttribute("totalPrice",totalPrice);
            model.addAttribute("numbers",numbers);
            return "cart";
        }
    }
    @PostMapping("/addPet/{id}")
    public String addPetToCar(HttpSession httpSession, @PathVariable Long id, RedirectAttributes redirectAttributes){
        User user = (User)httpSession.getAttribute("user");
        if (user ==null){
            redirectAttributes.addFlashAttribute("message","登陆后才能加入购物车哦");
            return "redirect:/admin/loginToIndex";
        }else {
            shopService.addPet(petService.getOnePet(id),user.getShopCar());
            redirectAttributes.addFlashAttribute("message","添加成功!");
            return "redirect:/index";
        }
    }

    @GetMapping("/delete/{id}")
    public String deleteItem(@PathVariable Long id,Model model,HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        shopService.deleteAItemFromCar(id,user.getShopCar().getId());
        model.addAttribute("message","删除成功");
        return "redirect:/car";
    }

    @PostMapping("/clearCar")
    public String clearCar(HttpSession session,RedirectAttributes redirectAttributes){
        User user = (User)session.getAttribute("user");
        Double totalPrice  =0.00;
        List<Pet> allItems = shopService.findAllItems(user.getShopCar().getId());
        for (Pet allItem : allItems) {
            totalPrice += allItem.getPrice();
        }
        if(totalPrice>user.getMoney()){
            redirectAttributes.addFlashAttribute("message","穷鬼，你的钱不够");
        }else {
            //扣钱
            Double newMoney = user.getMoney()- totalPrice;
            userService.changMoney(newMoney,user.getId());
            //商品卖出
            for (Pet allItem : allItems) {
                allItem.setOnSale(false);
            }
            //购物车清空
            shopService.clearCar(user);
            redirectAttributes.addFlashAttribute("message","恭喜，购买成功");
            //增加订单
            Order order = new Order();
            order.setUser(user);
            order.setPayDate(new Date());
            order.setPets(allItems);
            order.setTotalMoney(totalPrice);
            orderService.saveOrder(order);

        }
        return "redirect:/car";
    }

}
